package com.runen.infinitemovement.tool.map;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.runen.infinitemovement.App;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.AllPathSectionEntity;
import com.runen.infinitemovement.model.entity.MyLongRoadEntity;
import com.runen.infinitemovement.model.entity.PathSectionListEntity;
import com.runen.infinitemovement.model.entity.PolyLineEntity;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class MapDrawUtils {
    public static final String COLOR_RED = "#ED2122";
    public static final String COLOR_RED_30_ALPHA = "#EFA79A";
    public static final String COLOR_BLUE_ALPHA = "#2F64C4";
    public static final String COLOR_BLUE_14_ALPHA = "#90A2C9";
    public static final String COLOR_GREEN_ALPHA = "#34835B";
    public static final String COLOR_GREEN_14_ALPHA = "#92B295";
    public static final String COLOR_PURPLE_ALPHA = "#B345D6";
    public static final String COLOR_PURPLE_14_ALPHA = "#D293D2";

    public static final String COLOR_BLUE_14_ALPHA_1 = "#D5D1CD";
    public static final String COLOR_GREEN_14_ALPHA_1 = "#D6D5BE";
    public static final String COLOR_PURPLE_14_ALPHA_1 = "#E8CCD0";

    private static LatLngBounds.Builder newBounds = new LatLngBounds.Builder();

    public static List<PolyLineEntity> setMapData(AMap aMap, MyLongRoadEntity.DataBean data, List<AllPathSectionEntity.DataBean> datas, String headImgUrl) {
        Log.e("MapDrawUtils", "setMapData: start" + System.currentTimeMillis());
        if (data == null) return null;
        if (datas == null) return null;
        //由于颜色区分，先取出路线id
        List<String> pathIdList = new ArrayList<>();
        List<PolyLineEntity> polyLineEntityList = new ArrayList<>();
        for (AllPathSectionEntity.DataBean dataBean : datas) {
            if (!pathIdList.contains(dataBean.pathId)) {
                pathIdList.add(dataBean.pathId);
            }
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean latLng : dataBean.sectionLatitudeAndLongitudeDtoList) {
                newBounds.include(new LatLng(Double.parseDouble(latLng.latitude), Double.parseDouble(latLng.longitude)));
            }
        }
        if (aMap != null) {
            aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(newBounds.build(), 100), null);
        }
        //正在完成的路段 路段两端设置旗帜
        List<LatLng> flagList = new ArrayList<>();

        //每条路线所有的起终点经纬度
        List<LatLng> allstartAndEndList1 = new ArrayList<>();
        List<LatLng> allstartAndEndList2 = new ArrayList<>();
        List<LatLng> allstartAndEndList3 = new ArrayList<>();
        List<LatLng> allstartAndEndList4 = new ArrayList<>();

        //每条路线 所有已完成的起终点经纬度
        List<LatLng> allstarList1 = new ArrayList<>();
        List<LatLng> allstarList2 = new ArrayList<>();
        List<LatLng> allstarList3 = new ArrayList<>();
        List<LatLng> allstarList4 = new ArrayList<>();

        //每条路线 所有未完成的起终点经纬度
        List<LatLng> allLockList1 = new ArrayList<>();
        List<LatLng> allLockList2 = new ArrayList<>();
        List<LatLng> allLockList3 = new ArrayList<>();
        List<LatLng> allLockList4 = new ArrayList<>();

        List<AllPathSectionEntity.DataBean> bean1 = new ArrayList<>();
        List<AllPathSectionEntity.DataBean> bean2 = new ArrayList<>();
        List<AllPathSectionEntity.DataBean> bean3 = new ArrayList<>();
        List<AllPathSectionEntity.DataBean> bean4 = new ArrayList<>();

        for (AllPathSectionEntity.DataBean dataBean : datas) {
            if (dataBean.sectionId.equals(data.nowSectionInfoDTO.sectionId)) {
                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                flagList.add(startLatLng);
                flagList.add(endLatLng);
            }
            if (pathIdList.get(0).equals(dataBean.pathId)) {
                bean1.add(dataBean);

                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstartAndEndList1.contains(startLatLng))
                    allstartAndEndList1.add(startLatLng);
                if (!allstartAndEndList1.contains(endLatLng))
                    allstartAndEndList1.add(endLatLng);
            }
            if (pathIdList.get(1).equals(dataBean.pathId)) {
                bean2.add(dataBean);

                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstartAndEndList2.contains(startLatLng))
                    allstartAndEndList2.add(startLatLng);
                if (!allstartAndEndList2.contains(endLatLng))
                    allstartAndEndList2.add(endLatLng);
            }
            if (pathIdList.get(2).equals(dataBean.pathId)) {
                bean3.add(dataBean);

                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstartAndEndList3.contains(startLatLng))
                    allstartAndEndList3.add(startLatLng);
                if (!allstartAndEndList3.contains(endLatLng))
                    allstartAndEndList3.add(endLatLng);
            }
            if (pathIdList.get(3).equals(dataBean.pathId)) {
                bean4.add(dataBean);

                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstartAndEndList4.contains(startLatLng))
                    allstartAndEndList4.add(startLatLng);
                if (!allstartAndEndList4.contains(endLatLng))
                    allstartAndEndList4.add(endLatLng);
            }
        }

        //已完成路段绘制实线  未完成路段绘制虚线
        for (AllPathSectionEntity.DataBean bean : bean1) {
            List<LatLng> list = new ArrayList<>();
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean sectionBean : bean.sectionLatitudeAndLongitudeDtoList) {
                list.add(new LatLng(Double.parseDouble(sectionBean.latitude), Double.parseDouble(sectionBean.longitude)));
            }
            //已完成路段
            if (data.doneSectionId.contains(bean.sectionId)) {
                int size = bean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstarList1.contains(startLatLng))
                    allstarList1.add(startLatLng);
                if (!allstarList1.contains(endLatLng))
                    allstarList1.add(endLatLng);
                drawLine(aMap, list, COLOR_RED, false);
            } else { //未完成路段
                //当前路段是正在挑战的路段则不画线，反之画虚线
                if (!bean.sectionId.equals(data.nowSectionInfoDTO.sectionId))
                    drawLine(aMap, list, COLOR_RED_30_ALPHA, true);
            }
            PolyLineEntity polyLineEntity = new PolyLineEntity();
            polyLineEntity.sectionId = bean.sectionId;
            polyLineEntity.latLngs = list;
            polyLineEntityList.add(polyLineEntity);
        }
        for (AllPathSectionEntity.DataBean bean : bean2) {
            List<LatLng> list = new ArrayList<>();
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean sectionBean : bean.sectionLatitudeAndLongitudeDtoList) {
                list.add(new LatLng(Double.parseDouble(sectionBean.latitude), Double.parseDouble(sectionBean.longitude)));
            }
            //已完成路段
            if (data.doneSectionId.contains(bean.sectionId)) {
                int size = bean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstarList2.contains(startLatLng))
                    allstarList2.add(startLatLng);
                if (!allstarList2.contains(endLatLng))
                    allstarList2.add(endLatLng);
                drawLine(aMap, list, COLOR_BLUE_ALPHA, false);
            } else { //未完成路段
                //当前路段是正在挑战的路段则不画线，反之画虚线
                if (!bean.sectionId.equals(data.nowSectionInfoDTO.sectionId))
                    drawLine(aMap, list, COLOR_BLUE_14_ALPHA, true);
            }
            PolyLineEntity polyLineEntity = new PolyLineEntity();
            polyLineEntity.sectionId = bean.sectionId;
            polyLineEntity.latLngs = list;
            polyLineEntityList.add(polyLineEntity);
        }
        for (AllPathSectionEntity.DataBean bean : bean3) {
            List<LatLng> list = new ArrayList<>();
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean sectionBean : bean.sectionLatitudeAndLongitudeDtoList) {
                list.add(new LatLng(Double.parseDouble(sectionBean.latitude), Double.parseDouble(sectionBean.longitude)));
            }
            //已完成路段
            if (data.doneSectionId.contains(bean.sectionId)) {
                int size = bean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstarList3.contains(startLatLng))
                    allstarList3.add(startLatLng);
                if (!allstarList3.contains(endLatLng))
                    allstarList3.add(endLatLng);
                drawLine(aMap, list, COLOR_GREEN_ALPHA, false);
            } else { //未完成路段
                //当前路段是正在挑战的路段则不画线，反之画虚线
                if (!bean.sectionId.equals(data.nowSectionInfoDTO.sectionId))
                    drawLine(aMap, list, COLOR_GREEN_14_ALPHA, true);
            }
            PolyLineEntity polyLineEntity = new PolyLineEntity();
            polyLineEntity.sectionId = bean.sectionId;
            polyLineEntity.latLngs = list;
            polyLineEntityList.add(polyLineEntity);
        }
        for (AllPathSectionEntity.DataBean bean : bean4) {
            List<LatLng> list = new ArrayList<>();
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean sectionBean : bean.sectionLatitudeAndLongitudeDtoList) {
                list.add(new LatLng(Double.parseDouble(sectionBean.latitude), Double.parseDouble(sectionBean.longitude)));
            }
            //已完成路段
            if (data.doneSectionId.contains(bean.sectionId)) {
                int size = bean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstarList4.contains(startLatLng))
                    allstarList4.add(startLatLng);
                if (!allstarList4.contains(endLatLng))
                    allstarList4.add(endLatLng);
                drawLine(aMap, list, COLOR_PURPLE_ALPHA, false);
            } else { //未完成路段
                //当前路段是正在挑战的路段则不画线，反之画虚线
                if (!bean.sectionId.equals(data.nowSectionInfoDTO.sectionId))
                    drawLine(aMap, list, COLOR_PURPLE_14_ALPHA, true);
            }
            PolyLineEntity polyLineEntity = new PolyLineEntity();
            polyLineEntity.sectionId = bean.sectionId;
            polyLineEntity.latLngs = list;
            polyLineEntityList.add(polyLineEntity);
        }

        //未完成路段 起终点经纬度
        for (LatLng latLng : allstartAndEndList1) {
            if (allstarList1.size() != 0)
                for (LatLng latLng1 : allstarList1) {
                    if (latLng.latitude == latLng1.latitude &&
                            latLng.longitude == latLng1.longitude) {

                    } else {
                        if (!allLockList1.contains(latLng) && !allstarList1.contains(latLng)) {
                            allLockList1.add(latLng);
                        }
                    }
                }
            else {
                allLockList1.addAll(allstartAndEndList1);
            }
        }
        for (LatLng latLng : allstartAndEndList2) {
            if (allstarList2.size() != 0)
                for (LatLng latLng1 : allstarList2) {
                    if (latLng.latitude == latLng1.latitude &&
                            latLng.longitude == latLng1.longitude) {

                    } else {
                        if (!allLockList2.contains(latLng) && !allstarList2.contains(latLng)) {
                            allLockList2.add(latLng);
                        }
                    }
                }
            else {
                allLockList2.addAll(allstartAndEndList2);
            }
        }
        for (LatLng latLng : allstartAndEndList3) {
            if (allstarList3.size() != 0)
                for (LatLng latLng1 : allstarList3) {
                    if (latLng.latitude == latLng1.latitude &&
                            latLng.longitude == latLng1.longitude) {

                    } else {
                        if (!allLockList3.contains(latLng) && !allstarList3.contains(latLng)) {
                            allLockList3.add(latLng);
                        }
                    }
                }
            else {
                allLockList3.addAll(allstartAndEndList3);
            }
        }
        for (LatLng latLng : allstartAndEndList4) {
            if (allstarList4.size() != 0)
                for (LatLng latLng1 : allstarList4) {
                    if (latLng.latitude == latLng1.latitude &&
                            latLng.longitude == latLng1.longitude) {

                    } else {
                        if (!allLockList4.contains(latLng) && !allstarList4.contains(latLng)) {
                            allLockList4.add(latLng);
                        }
                    }
                }
            else {
                allLockList4.addAll(allstartAndEndList4);
            }
        }

        for (LatLng latLng : allstarList1) {
            setMarker(aMap, latLng, R.mipmap.ico_finish_pink);
        }
        for (LatLng latLng : allstarList2) {
            setMarker(aMap, latLng, R.mipmap.ico_finish_blue);
        }
        for (LatLng latLng : allstarList3) {
            setMarker(aMap, latLng, R.mipmap.ico_finish_green);
        }
        for (LatLng latLng : allstarList4) {
            setMarker(aMap, latLng, R.mipmap.ico_finish_purple);
        }

        for (LatLng latLng : allLockList1) {
            setMarker(aMap, latLng, R.mipmap.ico_lock_pink);
        }
        for (LatLng latLng : allLockList2) {
            setMarker(aMap, latLng, R.mipmap.ico_lock_blue);
        }
        for (LatLng latLng : allLockList3) {
            setMarker(aMap, latLng, R.mipmap.ico_lock_green);
        }
        for (LatLng latLng : allLockList4) {
            setMarker(aMap, latLng, R.mipmap.ico_lock_purple);
        }
        for (LatLng latLng : flagList) {
            setMarkerFlag(aMap, latLng);
        }

        //当前正在挑战中的路线id
        String nowPathId = "";
        PathSectionListEntity pathSectionListEntity = null;
        for (int i = 0; i < data.pathPathSectionList.size(); i++) {
            for (int j = 0; j < data.pathPathSectionList.get(i).pathSectionList.size(); j++) {
                if (data.nowSectionInfoDTO.sectionId.equals(data.pathPathSectionList.get(i).pathSectionList.get(j).sectionId)) {
                    pathSectionListEntity = data.pathPathSectionList.get(i).pathSectionList.get(j);
                    nowPathId = data.pathPathSectionList.get(i).pathId;
                }
            }
        }
        List<AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean> sectionListBean = null;
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).sectionId.equals(pathSectionListEntity.sectionId)) {
                sectionListBean = datas.get(i).sectionLatitudeAndLongitudeDtoList;
            }
        }

        List<LatLng> fullLatLngs = new ArrayList<>();
        List<LatLng> xuLatLngs = new ArrayList<>();
        if (data.nowSectionInfoDTO != null && pathSectionListEntity != null) {
            if (data.nowSectionInfoDTO.sectionId.equals(pathSectionListEntity.sectionId)) {
                double lastDistance = pathSectionListEntity.mileage * data.nowSectionInfoDTO.completionRate;

                for (int i = 0; i < sectionListBean.size(); i++) {
                    if (lastDistance < Double.parseDouble(sectionListBean.get(i).mileage)) {
                        xuLatLngs.add(new LatLng(Double.parseDouble(sectionListBean.get(i).latitude),
                                Double.parseDouble(sectionListBean.get(i).longitude)));
                    } else {
                        fullLatLngs.add(new LatLng(Double.parseDouble(sectionListBean.get(i).latitude),
                                Double.parseDouble(sectionListBean.get(i).longitude)));
                    }
                }
                String color = COLOR_RED;
                String xuColor = COLOR_RED_30_ALPHA;
                for (int i = 0; i < pathIdList.size(); i++) {
                    if (nowPathId.equals(pathIdList.get(i))) {
                        switch (i) {
                            case 0:
                                color = COLOR_RED;
                                xuColor = COLOR_RED_30_ALPHA;
                                break;
                            case 1:
                                color = COLOR_BLUE_ALPHA;
                                xuColor = COLOR_BLUE_14_ALPHA;
                                break;
                            case 2:
                                color = COLOR_GREEN_ALPHA;
                                xuColor = COLOR_GREEN_14_ALPHA;
                                break;
                            case 3:
                                color = COLOR_PURPLE_ALPHA;
                                xuColor = COLOR_PURPLE_14_ALPHA;
                                break;
                        }
                    }
                }
                if (fullLatLngs != null && fullLatLngs.size() > 0) {
                    xuLatLngs.add(0, fullLatLngs.get(fullLatLngs.size() - 1));
                    setMarker2(aMap, fullLatLngs.get(fullLatLngs.size() - 1).latitude, fullLatLngs.get(fullLatLngs.size() - 1).longitude, headImgUrl);
                    drawLine(aMap, fullLatLngs, color, false);
                }
                if (xuLatLngs != null && xuLatLngs.size() > 0) {
                    drawLine(aMap, xuLatLngs, xuColor, true);
                }
            }
        }
        Log.e("MapDrawUtils", "setMapData: end" + System.currentTimeMillis());
        allLockList1.clear();
        allLockList2.clear();
        allLockList3.clear();
        allLockList4.clear();
        allstarList1.clear();
        allstarList2.clear();
        allstarList3.clear();
        allstarList4.clear();
        allstartAndEndList1.clear();
        allstartAndEndList2.clear();
        allstartAndEndList3.clear();
        allstartAndEndList4.clear();
        pathIdList.clear();
        flagList.clear();
        bean1.clear();
        bean2.clear();
        bean3.clear();
        bean4.clear();
        return polyLineEntityList;
    }

    private static void setMarker(AMap aMap, LatLng latLng, int img) {
        aMap.addMarker(
                new MarkerOptions()
                        .position(latLng)
                        .anchor(0.5f, 0.5f)
                        //添加markerID
                        .draggable(false)
                        .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(App.mContext.getResources(), img))));
    }

    private static void setMarkerFlag(AMap aMap, LatLng latLng) {
        aMap.addMarker(
                new MarkerOptions()
                        .position(latLng)
                        .anchor(0.2f, 0.9f)
                        //添加markerID
                        .draggable(false)
                        .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(App.mContext.getResources(), R.mipmap.ico_flag))));
    }

    private static void drawLine(AMap aMap, List<LatLng> latLngs, String color, boolean dotted) {
        aMap.addPolyline(new PolylineOptions().
                addAll(latLngs)
                .width(dotted ? 13 : 15) //虚线13  实线15
                .setDottedLine(dotted) //打开虚线
                .color(Color.parseColor(color)));
    }

    private static void drawLine(AMap aMap, List<LatLng> latLngs, String color) {
        aMap.addPolyline(new PolylineOptions().
                addAll(latLngs)
                .width(10) //实线10
                .color(Color.parseColor(color)));
    }

    private static void setMarker2(AMap aMap, double lat, double lon, String headImgUrl) {
        View view = View.inflate(App.getInstance().getApplicationContext(), R.layout.view_custom_mark, null);
        RoundedCornersImage rc_userHeader = view.findViewById(R.id.rc_userHeader);
        TextView tv_company_name = view.findViewById(R.id.tv_company_name);
        Glide.with(App.getInstance().getApplicationContext()).load(headImgUrl).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                rc_userHeader.setImageDrawable(resource);
                aMap.addMarker(
                        new MarkerOptions()
                                .position(new LatLng(lat, lon))
                                .anchor(0.5f, 0.55f)
                                //添加markerID
                                .draggable(false).icon(BitmapDescriptorFactory.fromView(view)));
            }
        });
    }

    public static void setCompanyMapData(AMap aMap, List<AllPathSectionEntity.DataBean> datas,
                                         String nowSectionId, double sectionLastDistance, List<String> doneSectionId,
                                         PathSectionListEntity pathSectionListEntity) {
        if (datas == null) return;
        //由于颜色区分，先取出路线id
        List<String> pathIdList = new ArrayList<>();

        for (AllPathSectionEntity.DataBean dataBean : datas) {
            if (!pathIdList.contains(dataBean.pathId)) {
                pathIdList.add(dataBean.pathId);
            }
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean latLng : dataBean.sectionLatitudeAndLongitudeDtoList) {
                newBounds.include(new LatLng(Double.parseDouble(latLng.latitude), Double.parseDouble(latLng.longitude)));
            }
        }
        if (aMap != null) {
            aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(newBounds.build(), 100), null);
        }

        //正在完成的路段 路段两端设置旗帜
        List<LatLng> flagList = new ArrayList<>();

        //每条路线所有的起终点经纬度
        List<LatLng> allstartAndEndList1 = new ArrayList<>();
        List<LatLng> allstartAndEndList2 = new ArrayList<>();
        List<LatLng> allstartAndEndList3 = new ArrayList<>();
        List<LatLng> allstartAndEndList4 = new ArrayList<>();

        //第一路线 所有已完成的起终点经纬度
        List<LatLng> allstarList1 = new ArrayList<>();

        //第一路线 所有未完成的起终点经纬度
        List<LatLng> allLockList1 = new ArrayList<>();

        //每条路段的经纬度
        List<AllPathSectionEntity.DataBean> bean1 = new ArrayList<>();
        List<AllPathSectionEntity.DataBean> bean2 = new ArrayList<>();
        List<AllPathSectionEntity.DataBean> bean3 = new ArrayList<>();
        List<AllPathSectionEntity.DataBean> bean4 = new ArrayList<>();

        for (AllPathSectionEntity.DataBean dataBean : datas) {
            if (dataBean.sectionId.equals(nowSectionId)) {
                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                flagList.add(startLatLng);
                flagList.add(endLatLng);
            }

            if (pathIdList.get(0).equals(dataBean.pathId)) {
                bean1.add(dataBean);

                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstartAndEndList1.contains(startLatLng))
                    allstartAndEndList1.add(startLatLng);
                if (!allstartAndEndList1.contains(endLatLng))
                    allstartAndEndList1.add(endLatLng);
            }
            if (pathIdList.get(1).equals(dataBean.pathId)) {
                bean2.add(dataBean);

                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstartAndEndList2.contains(startLatLng))
                    allstartAndEndList2.add(startLatLng);
                if (!allstartAndEndList2.contains(endLatLng))
                    allstartAndEndList2.add(endLatLng);
            }
            if (pathIdList.get(2).equals(dataBean.pathId)) {
                bean3.add(dataBean);

                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstartAndEndList3.contains(startLatLng))
                    allstartAndEndList3.add(startLatLng);
                if (!allstartAndEndList3.contains(endLatLng))
                    allstartAndEndList3.add(endLatLng);
            }
            if (pathIdList.get(3).equals(dataBean.pathId)) {
                bean4.add(dataBean);

                int size = dataBean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(dataBean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstartAndEndList4.contains(startLatLng))
                    allstartAndEndList4.add(startLatLng);
                if (!allstartAndEndList4.contains(endLatLng))
                    allstartAndEndList4.add(endLatLng);
            }
        }

        //已完成路段绘制实线  未完成路段绘制虚线
        for (AllPathSectionEntity.DataBean bean : bean1) {
            List<LatLng> list = new ArrayList<>();
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean sectionBean : bean.sectionLatitudeAndLongitudeDtoList) {
                list.add(new LatLng(Double.parseDouble(sectionBean.latitude), Double.parseDouble(sectionBean.longitude)));
            }
            //已完成路段
            if (doneSectionId.contains(bean.sectionId)) {
                int size = bean.sectionLatitudeAndLongitudeDtoList.size();
                LatLng startLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(0).longitude));
                LatLng endLatLng = new LatLng(Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).latitude),
                        Double.parseDouble(bean.sectionLatitudeAndLongitudeDtoList.get(size - 1).longitude));
                if (!allstarList1.contains(startLatLng))
                    allstarList1.add(startLatLng);
                if (!allstarList1.contains(endLatLng))
                    allstarList1.add(endLatLng);
                drawLine(aMap, list, COLOR_RED, false);
            } else { //未完成路段
                //当前路段是正在挑战的路段则不画线，反之画虚线
                if (!bean.sectionId.equals(nowSectionId))
                    drawLine(aMap, list, COLOR_RED_30_ALPHA, true);
            }
//            PolyLineEntity polyLineEntity = new PolyLineEntity();
//            polyLineEntity.sectionId = bean.sectionId;
//            polyLineEntity.latLngs = list;
//            polyLineEntityList.add(polyLineEntity);
        }
        for (AllPathSectionEntity.DataBean bean : bean2) {
            List<LatLng> list = new ArrayList<>();
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean sectionBean : bean.sectionLatitudeAndLongitudeDtoList) {
                list.add(new LatLng(Double.parseDouble(sectionBean.latitude), Double.parseDouble(sectionBean.longitude)));
            }
            //直接画实线
            drawLine(aMap, list, COLOR_BLUE_14_ALPHA_1);

            PolyLineEntity polyLineEntity = new PolyLineEntity();
            polyLineEntity.sectionId = bean.sectionId;
            polyLineEntity.latLngs = list;
//            polyLineEntityList.add(polyLineEntity);
        }
        for (AllPathSectionEntity.DataBean bean : bean3) {
            List<LatLng> list = new ArrayList<>();
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean sectionBean : bean.sectionLatitudeAndLongitudeDtoList) {
                list.add(new LatLng(Double.parseDouble(sectionBean.latitude), Double.parseDouble(sectionBean.longitude)));
            }
            //直接画实线
            drawLine(aMap, list, COLOR_GREEN_14_ALPHA_1);

            PolyLineEntity polyLineEntity = new PolyLineEntity();
            polyLineEntity.sectionId = bean.sectionId;
            polyLineEntity.latLngs = list;
//            polyLineEntityList.add(polyLineEntity);
        }
        for (AllPathSectionEntity.DataBean bean : bean4) {
            List<LatLng> list = new ArrayList<>();
            for (AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean sectionBean : bean.sectionLatitudeAndLongitudeDtoList) {
                list.add(new LatLng(Double.parseDouble(sectionBean.latitude), Double.parseDouble(sectionBean.longitude)));
            }
            //直接画实线
            drawLine(aMap, list, COLOR_PURPLE_14_ALPHA_1);

            PolyLineEntity polyLineEntity = new PolyLineEntity();
            polyLineEntity.sectionId = bean.sectionId;
            polyLineEntity.latLngs = list;
//            polyLineEntityList.add(polyLineEntity);
        }

        //未完成路段 起终点经纬度
        for (LatLng latLng : allstartAndEndList1) {
            if (allstarList1.size() != 0)
                for (LatLng latLng1 : allstarList1) {
                    if (latLng.latitude == latLng1.latitude &&
                            latLng.longitude == latLng1.longitude) {

                    } else {
                        if (!allLockList1.contains(latLng) && !allstarList1.contains(latLng)) {
                            allLockList1.add(latLng);
                        }
                    }
                }
            else {
                allLockList1.addAll(allstartAndEndList1);
            }
        }

        for (LatLng latLng : allstarList1) {
            setMarker(aMap, latLng, R.mipmap.ico_company_finish_pink);
        }
        for (LatLng latLng : allstartAndEndList2) {
            setMarker(aMap, latLng, R.mipmap.ico_company_marker_blue);
        }
        for (LatLng latLng : allstartAndEndList3) {
            setMarker(aMap, latLng, R.mipmap.ico_company_marker_green);
        }
        for (LatLng latLng : allstartAndEndList4) {
            setMarker(aMap, latLng, R.mipmap.ico_company_marker_purple);
        }
        for (LatLng latLng : allLockList1) {
            setMarker(aMap, latLng, R.mipmap.ico_company_lock_pink);
        }

        List<AllPathSectionEntity.DataBean.SectionLatitudeAndLongitudeDtoListBean> sectionListBean = null;
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).sectionId.equals(pathSectionListEntity.sectionId)) {
                sectionListBean = datas.get(i).sectionLatitudeAndLongitudeDtoList;
            }
        }

        List<LatLng> fullLatLngs = new ArrayList<>();
        List<LatLng> xuLatLngs = new ArrayList<>();
        if (pathSectionListEntity != null) {
            if (nowSectionId.equals(pathSectionListEntity.sectionId)) {
                for (int i = 0; i < sectionListBean.size(); i++) {
                    if (sectionLastDistance < Double.parseDouble(sectionListBean.get(i).mileage)) {
                        xuLatLngs.add(new LatLng(Double.parseDouble(sectionListBean.get(i).latitude),
                                Double.parseDouble(sectionListBean.get(i).longitude)));
                    } else {
                        fullLatLngs.add(new LatLng(Double.parseDouble(sectionListBean.get(i).latitude),
                                Double.parseDouble(sectionListBean.get(i).longitude)));
                    }
                }
                String color = COLOR_RED;
                String xuColor = COLOR_RED_30_ALPHA;

                if (fullLatLngs != null && fullLatLngs.size() > 0) {
                    xuLatLngs.add(0, fullLatLngs.get(fullLatLngs.size() - 1));
//                    setMarker2(aMap, fullLatLngs.get(fullLatLngs.size() - 1).latitude, fullLatLngs.get(fullLatLngs.size() - 1).longitude, headImgUrl);
                    drawLine(aMap, fullLatLngs, color, false);
                }
                if (xuLatLngs != null && xuLatLngs.size() > 0) {
                    drawLine(aMap, xuLatLngs, xuColor, true);
                }
            }
        }

        allLockList1.clear();
        allstarList1.clear();
        allstartAndEndList1.clear();
        allstartAndEndList2.clear();
        allstartAndEndList3.clear();
        allstartAndEndList4.clear();
        pathIdList.clear();
        flagList.clear();
        bean1.clear();
        bean2.clear();
        bean3.clear();
        bean4.clear();
    }
}
